package com.typing.alarm.database;

import java.io.*;
import java.util.*;

import com.typing.alarm.value.TypingAlarmVO;

import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.util.*;

public class DBManager {

  private static final String TAG                 = "DBManager";
    
  public static final String DATABASE_NAME       = "clothDataBase.db";
  public static final String DATABASE_TABLE      = "ALARM_INFO";
  public static final int    DATABASE_VERSION    = 1;
  
  public static final String  KEY_ID                   = "ALARM_ID";
  public static final String  KEY_NAME          	   = "ALARM_NAME";
  public static final String  KEY_TIME     			   = "ALARM_TIME";
  public static final String  KEY_REPEAT_YN     	   = "REPEAT_YN";
  public static final String  KEY_REPEAT_INTERVAL      = "REPEAT_INTERVAL";
  public static final String  KEY_REPEAT_DAY   	       = "REPEAT_DAY";
  public static final String  KEY_RING_TYPE     	   = "RING_TYPE";
  public static final String  KEY_RING_SOUND           = "RING_SOUND";
  public static final String  KEY_TYPING_TEXT          = "TYPING_TEXT";
  public static final String  KEY_TYPING_SPEED         = "TYPING_SPEED";
  public static final String  KEY_USE_YN               = "USE_YN";
  
  
  private final String DATABASE_CREATE     = 
      "create table "+ DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement," +
    		  KEY_NAME + " varchar not null," +
    		  KEY_TIME + " varchar ," +
    		  KEY_REPEAT_YN + " char ," +
    		  KEY_REPEAT_INTERVAL + " varchar ," +
    		  KEY_REPEAT_DAY + " varchar ," +
    		  KEY_RING_TYPE + " char ," +
    		  KEY_RING_SOUND + " varchar ," +
    		  KEY_TYPING_TEXT + " varchar ," +
    		  KEY_TYPING_SPEED + " varchar ," +
    		  KEY_USE_YN + " char not null" + ");";        					  

    private SQLHelper mDbHelper;
    private SQLiteDatabase mDb;
    private Cursor mCursor;
    private final Context  mContext;
    private ArrayList<TypingAlarmVO> list = new ArrayList<TypingAlarmVO>();
    public DBManager(Context context) {
        mContext = context;
    }
    
    public static boolean isExistDB() {
        boolean result = false;
        File file = new File("/data/data/com.typing.alarm/databases/"+DATABASE_NAME);
                
        if (file.exists()){
            result = true;
        } else {
            Log.d("DBAdapter", "DB does not exist!!!!!!");
        }
        return result;
    }
        
    public SQLiteDatabase open() {
//        mDbHelper = new SQLHelper(mContext);
		mDbHelper = new SQLHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
		mDbHelper.setDataBaseInfo(DATABASE_TABLE, DATABASE_CREATE);
        mDb = mDbHelper.getWritableDatabase();
        return mDb;
    }
    
    public void close() {
        mDbHelper.close();
    }
    public ArrayList<TypingAlarmVO> getAppInfo(String sql,String param) {
    	try{
	    	mCursor = mDb.rawQuery( sql, null );
	    	if ( mCursor != null )
	    	{
	    		if ( mCursor.moveToFirst() )
	    		{
	    			String strRow = "--------------------------------\n";
	    			for(int i = 0 ; i < mCursor.getColumnCount() ; i++ )
	    			{
	    				strRow += mCursor.getColumnName(i) + " | ";
	    			}
	    			strRow += "\n";
	    			Log.d("","colums===>>"+ strRow );
	
	    			do
	    			{
	    				TypingAlarmVO pdata = new TypingAlarmVO();
	    				strRow = "";
	    				for(int i = 0 ; i < mCursor.getColumnCount() ; i++ )
	    				{
	    					pdata=setAlarmData(mCursor.getColumnName(i),mCursor.getString(i),pdata);
	    					strRow += mCursor.getString(i) + " | ";
	    				}
	    				strRow += "\n";
	    				Log.d("","DB result===>>"+  strRow );
	    				list.add(pdata);
	    			} while ( mCursor.moveToNext() );
	    		}
	    	}
	    	mCursor.close(); // 커서 닫기
		}catch ( SQLException e )
		{
			// SQL 예외 처리
			Log.e( "SQLException", e.getMessage() );
		}
		finally
		{
			// Database 닫기
			mDb.close();
			Log.i( "SQLite", "Database Close ... OK" );
		}

    	return list;
    }
    public TypingAlarmVO setAlarmData(String key ,String val,TypingAlarmVO data){    	
    	if(key.equals(KEY_ID))
    		data.setAlarmId(val);
    	else if(key.equals(KEY_NAME))
    		data.setAlarmName(val);
    	else if(key.equals(KEY_TIME))
    		data.setAlarmTime(val);
    	else if(key.equals(KEY_REPEAT_YN))
    		data.setRepeatYn(val);
    	else if(key.equals(KEY_REPEAT_INTERVAL))
    		data.setRepeatInterval(val);
    	else if(key.equals(KEY_REPEAT_DAY))
    		data.setRepeatDayByString(val);
    	else if(key.equals(KEY_RING_TYPE))
    		data.setRingType(val);
    	else if(key.equals(KEY_RING_SOUND))
    		data.setRingSound(val);
    	else if(key.equals(KEY_TYPING_TEXT))
    		data.setTypingText(val);
    	else if(key.equals(KEY_TYPING_SPEED))
    		data.setTypingSpeed(val);
    	else if(key.equals(KEY_USE_YN))
    		data.setUseYn(val);
    	
    	return data;
    }
    public long addAlarmInfo(TypingAlarmVO data) {
        ContentValues v = new ContentValues();     
        v.put(KEY_ID, data.getAlarmId());
        v.put(KEY_NAME, data.getAlarmName());
        v.put(KEY_TIME, data.getAlarmTime());
        v.put(KEY_REPEAT_YN, data.getRepeatYn());
        v.put(KEY_REPEAT_INTERVAL, data.getRepeatInterval());
        v.put(KEY_REPEAT_DAY, data.getRepeatDayToString());
        v.put(KEY_RING_TYPE, data.getRingType());
        v.put(KEY_RING_SOUND, data.getRingSound());
        v.put(KEY_TYPING_TEXT, data.getTypingText());
        v.put(KEY_TYPING_SPEED, data.getTypingSpeed());
        v.put(KEY_USE_YN, data.getUseYn());
        return mDb.insert(DATABASE_TABLE, null, v);
    }
    
    public long updateAlarmInfo(TypingAlarmVO data) {    	
        ContentValues v = new ContentValues();         
        v.put(KEY_NAME, data.getAlarmName());
        v.put(KEY_TIME, data.getAlarmTime());
        v.put(KEY_REPEAT_YN, data.getRepeatYn());
        v.put(KEY_REPEAT_INTERVAL, data.getRepeatInterval());
        v.put(KEY_REPEAT_DAY, data.getRepeatDayToString());
        v.put(KEY_RING_TYPE, data.getRingType());
        v.put(KEY_RING_SOUND, data.getRingSound());
        v.put(KEY_TYPING_TEXT, data.getTypingText());
        v.put(KEY_TYPING_SPEED, data.getTypingSpeed());
        v.put(KEY_USE_YN, data.getUseYn());
        return mDb.update(DATABASE_TABLE , v , KEY_ID+" = "+data.getAlarmId() , null);
    }
    
    public boolean deleteAlarmInfo(TypingAlarmVO appInfo) {
        return mDb.delete(DATABASE_TABLE, KEY_ID + "=" + appInfo.getAlarmId(), null) > 0; 
    }
    
    public Cursor fetchAllAlarmInfos(String where) {      
        String query = "SELECT * FROM " + DATABASE_TABLE + where + " ORDER BY " + KEY_ID + " ASC";
        return mDb.rawQuery(query, null);
    }
    
//    public boolean updateAppInfos(ArrayList<AppInfo> appInfoList) {
//        boolean ret = true;
//        for (int i = 0; i < appInfoList.size(); i++) {
//            AppInfo appInfo = appInfoList.get(i);
//            ContentValues v = new ContentValues();
//            v.put(KEY_ORDER, i);
//            v.put(KEY_VISIBILITY, appInfo.getVisibility());
//            
//            String where = KEY_ID+"="+appInfo.getAppId();
//            
//            ret &= (mDb.update(DATABASE_TABLE, v, where, null) > 0); 
//        }
//        if (!ret) {
//            Log.w(TAG, "updateAppInfos() - failed!");
//        }
//        return ret;
//    }
    
}
